/* eslint comma-dangle: "off" */
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CompileEventsPlugin = require('./build/compile-events-plugin')
const filterRemove = require('./build/filter-remove')
const ScriptPlugin = require('./build/script-plugin')
const merge = require('webpack-merge')
const getProdConf = require('./build/webpack.prod')

// 命令行参数
// const argv = require('yargs').argv
// const isTest = argv['test'] === true // 有时候可能需要测试编译结果

let outputPath = path.resolve('D:/_work/ccard.shendupeiban.com/src/ccard.shendupeiban.com/ccard.shendupeiban.com/Areas/Mccard/Content/ccard')
try {
  fs.accessSync(outputPath)
} catch (err) {
  // 文件和目录不存在
  if (err.code === 'ENOENT') {
    outputPath = path.resolve(__dirname, 'dist')
  }
}

console.log(outputPath)
const baseProdConf = getProdConf({
  splitCss: true, // css 拆分
  sourceMap: false, // 默认为 true
  // 更改入口 index template
  indexTemplate () {
    return new HtmlWebpackPlugin({
      // filename: isTest ? 'index.html' : '../../Views/Default/Index.cshtml',
      filename: 'index.html',
      template: './src/index.html',
      inlineSource: /(styles\.bundle\.js|\.css$)/,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        // minifyCSS: true,
        // minifyJS: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      }
    })
  }
})

const prodConf = {
  // watch: true,
  // devtool: 'source-map',
  // 不打包的模块
  // 键为 import 调用名，值为全局名称
  externals: {
    'vue': 'Vue'
    // 'vue-router': 'VueRouter'
  },
  output: {
    path: outputPath
  },
  plugins: [
    // 将抽离的 css、js 包含进 html 文件
    new HtmlWebpackInlineSourcePlugin(),
    // 编译事件
    new CompileEventsPlugin({
      compile () {
        // 编译前删除
        filterRemove(outputPath, /^(fonts|js|imgs)$/)
      },
      done () {
        // 编译后删除
        filterRemove(outputPath, /\.(css|css\.map)$/)
        filterRemove(outputPath + '/js', /^styles\.bundle\.js$/)
      }
    }),
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, 'node_modules/vue/dist/vue.runtime.min.js'),
        // to: path.resolve(outputPath, '../', 'js/vue.min.js'),
        to: path.resolve(outputPath, '', 'js/vue.runtime.min.js'),
      }
    ]),
    // 添加指定的 cdn 包。或者指定路径的包也行
    new ScriptPlugin([
      // isTest ? 'js/vue.min.js' : '/Areas/Mccard/Content/js/vue.min.js',
      'js/vue.runtime.min.js',
      // 'http://style.shenduxuetang.com/vue/2.5.13/vue.min.js',
      // 'http://style.shenduxuetang.com/vue-router/3.0.1/vue-router.min.js'
    ]),
    // 打包分析
    // new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)({
    //   analyzerMode: 'static',
    //   openAnalyzer: false,
    //   reportFilename: path.resolve(__dirname, 'dist-report/report.html')
    // })
  ]
}

webpack(merge(baseProdConf, prodConf), require('./build/msg-webpack'))
